package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.dto.InvitationRankDto;
import com.vf.admin.client.service.AppInvitationRecordService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.miaodong.entity.AppInvitationRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/app/invitation")
public class InvitationController extends ClientBaseController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppInvitationRecordService appInvitationRecordService;

    /**
     * 收益明细
     * @return
     */
    @RequestMapping(value = "/inviteDetail")
    public Result inviteDetail() {
        try {
            List<AppInvitationRecord> yaoqingList = appInvitationRecordService.findByYaoqingList(getUserId());
            for(AppInvitationRecord invitationRecord : yaoqingList) {
                BigDecimal bigDecimal = new BigDecimal(invitationRecord.getSupermoney().toString()).divide(new BigDecimal("10")).setScale(2, BigDecimal.ROUND_HALF_UP);
                invitationRecord.setSupermoney(bigDecimal.doubleValue());
                //invitationRecord.setRmb(bigDecimal.stripTrailingZeros().toPlainString());
            }
            return this.sucessJSON(yaoqingList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 我的邀请
     * @return
     */
    @RequestMapping(value = "/myInviteSonUser")
    public Result myInviteSonUser() {
        try {
            List<AppInvitationRecord> invitationRecordList = appInvitationRecordService.myInviteSonUser(getUserId());
            return this.sucessJSON(invitationRecordList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 奖励排行
     * @return
     */
    @RequestMapping(value = "/inviteRank")
    @ResponseBody
    public Result inviteRank() {
        try {
            List<InvitationRankDto> invitationRankDtos = appInvitationRecordService.inviteRank();
            for(InvitationRankDto invitation : invitationRankDtos) {
                BigDecimal bigDecimal = new BigDecimal(invitation.getMoney()).divide(new BigDecimal("10")).setScale(2, BigDecimal.ROUND_HALF_UP);
                invitation.setMoney(bigDecimal.stripTrailingZeros().toPlainString());
            }
            return this.sucessJSON(invitationRankDtos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
